package smoothcraft.Util.spawn;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

public class Main extends smoothcraft.API implements CommandExecutor{
	@Override
	public boolean onCommand(CommandSender sender, Command cmd, String label, String[] arg) {
		if(arg.length == 0) {
			if(sender instanceof Player) {
				Player p = (Player)sender;
				p.teleport(getSpawnLoc());
				sender.sendMessage(getMessage("spawn.spawn"));
				return true;
			}
			return true;
		}
		if(arg[0].equals("set")) {
			if(sender instanceof Player) {
				Player p = (Player)sender;
				saveSpawnLoc(p.getLocation());
				sender.sendMessage(getMessage("spawn.setspawn"));
				return true;
			}
			return true;
		}
		return false;
	}
	public Location getSpawnLoc() {
		String world = getConfigString("Loc.spawn.world");
		double x = getConfigDouble("Loc.spawn.x");
		double y = getConfigDouble("Loc.spawn.y");
		double z = getConfigDouble("Loc.spawn.z");
		float  yaw = getConfigInt("Loc.spawn.yaw");
		float pitch = getConfigInt("Loc.spawn.pitch");
		Location loc = new Location(Bukkit.getWorld(world), x, y, z, yaw, pitch);
		return loc;
	}
	public void saveSpawnLoc(Location loc) {
		setConfig("Loc.spawn.x",loc.getX());
		setConfig("Loc.spawn.y",loc.getY());
		setConfig("Loc.spawn.z",loc.getZ());
		setConfig("Loc.spawn.yaw",loc.getYaw());
		setConfig("Loc.spawn.pitch",loc.getPitch());
		setConfig("Loc.spawn.world",loc.getWorld().getName());
		save();
	}
}
